package wymiana.mbeans;

import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import wymiana.domain.DGroup;
import wymiana.domain.GroupRequest;
import wymiana.domain.Mail;
import wymiana.domain.User;
import wymiana.mbeans.group.GroupMBean;
import wymiana.service.GroupRequestService;
import wymiana.service.GroupService;
import wymiana.service.MailService;
import wymiana.service.UserService;

@ManagedBean(name = "groupRequestMBean")
// @RequestScoped
@SessionScoped
public class GroupRequestMBean {

	@ManagedProperty("#{groupService}")
	private GroupService groupService;

	@ManagedProperty("#{groupRequestService}")
	private GroupRequestService groupRequestService;

	@ManagedProperty("#{userMBean}")
	private UserBean userMBean;

	@ManagedProperty("#{groupMBean}")
	private GroupMBean groupMBean;

	@ManagedProperty("#{userService}")
	private UserService userService;

	@ManagedProperty("#{mailService}")
	private MailService mailService;

	private List<GroupRequest> myGroupRequest;

	private List<GroupRequest> myGroupRequestFiltered;

	public String myRequests() {
		return "myRequests";
	}

	public String accept() {
		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();

		String groupId = (String) externalContext.getRequestParameterMap().get(
				"groupId");

		GroupRequest gr = groupRequestService.getGroupRequest(Integer
				.parseInt(groupId));

		gr.setAccepted(true);

		Mail m = new Mail();
		m.setDate(new Date());
		m.setDeleted(false);
		m.setReaded(false);
		m.setTrash(false);
		m.setTrash_sender(false);
		m.setDeleted_sender(false);
		m.setContent("Zostałeś dodany do grupy: " + gr.getDgroup().getName());

		m.setTitle("Dodanie do grupy");
		User user = userMBean.getLoggedUser();
		m.setSender(user);
		m.setRecipient(gr.getAskingUser());
		mailService.sendMail(m);

		User toSave = userService.getUserByUsername(gr.getAskingUser().getUsername());
		DGroup grupa = groupService.getGroup(gr.getDgroup().getIdGroup());
		grupa.getUsers().add(toSave);
		toSave.getGroups().add(grupa);

		userService.update(toSave);
		groupService.updateGroup(grupa);
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
				"Sukces: ", "Zgłoszenie zaakceptowane");

		FacesContext.getCurrentInstance().addMessage(null, message);
		return "";
	}

	public String removeGroupReqest() {
		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();

		String groupId = (String) externalContext.getRequestParameterMap().get(
				"groupId");

		groupRequestService.deleteGroupRequestById(Integer.parseInt(groupId));

		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
				"Sukces: ", "Poprawnie usunąłeś zgłoszenie.");

		FacesContext.getCurrentInstance().addMessage(null, message);
		return "";
	}

	public String addRequest() {
		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();

		String id = (String) externalContext.getRequestParameterMap().get(
				"groupId");

		DGroup g = groupService.getGroup(Integer.parseInt(id));
		User u = userMBean.getLoggedUser();
		GroupRequest req = new GroupRequest();
		req.setAccepted(false);
		req.setAskingUser(u);
		req.setDgroup(g);

		boolean send = groupRequestService.create(req);

		if (send) {
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN,
					"Uwaga: ", "Wysłałeś już prośbę o dodanie do grupy.");

			FacesContext.getCurrentInstance().addMessage(null, message);
		} else {
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
					"Sukces: ", "Prośba o dołaczenie do grupy wysłana.");

			FacesContext.getCurrentInstance().addMessage(null, message);
		}

		return "";
	}

	public boolean isInGroup() {
		User u = userMBean.getLoggedUser();
		DGroup g = groupMBean.getActualGroup();
		g = groupService.getGroup(g.getIdGroup());

		for (User uu : g.getUsers()) {
			if (uu.getUsername().equals(u.getUsername())) {
				return true;
			}
		}
		if (g.getCreateUser() != null
				&& u.getUsername().equals(g.getCreateUser().getUsername())) {
			return true;
		}
		return false;
	}

	/**
	 * @return the groupService
	 */
	public GroupService getGroupService() {
		return groupService;
	}

	/**
	 * @param groupService
	 *            the groupService to set
	 */
	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	/**
	 * @return the userMBean
	 */
	public UserBean getUserMBean() {
		return userMBean;
	}

	/**
	 * @param userMBean
	 *            the userMBean to set
	 */
	public void setUserMBean(UserBean userMBean) {
		this.userMBean = userMBean;
	}

	/**
	 * @return the userService
	 */
	public UserService getUserService() {
		return userService;
	}

	/**
	 * @param userService
	 *            the userService to set
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * @return the myGroupRequest
	 */
	public List<GroupRequest> getMyGroupRequest() {
		User u = userMBean.getLoggedUser();
		List<GroupRequest> list = groupRequestService.getMyGroupRequest(u);

		return list;
	}

	/**
	 * @param myGroupRequest
	 *            the myGroupRequest to set
	 */
	public void setMyGroupRequest(List<GroupRequest> myGroupRequest) {
		this.myGroupRequest = myGroupRequest;
	}

	/**
	 * @return the groupRequestService
	 */
	public GroupRequestService getGroupRequestService() {
		return groupRequestService;
	}

	/**
	 * @param groupRequestService
	 *            the groupRequestService to set
	 */
	public void setGroupRequestService(GroupRequestService groupRequestService) {
		this.groupRequestService = groupRequestService;
	}

	/**
	 * @return the groupMBean
	 */
	public GroupMBean getGroupMBean() {
		return groupMBean;
	}

	/**
	 * @param groupMBean
	 *            the groupMBean to set
	 */
	public void setGroupMBean(GroupMBean groupMBean) {
		this.groupMBean = groupMBean;
	}

	/**
	 * @return the myGroupRequestFiltered
	 */
	public List<GroupRequest> getMyGroupRequestFiltered() {
		return myGroupRequestFiltered;
	}

	/**
	 * @param myGroupRequestFiltered
	 *            the myGroupRequestFiltered to set
	 */
	public void setMyGroupRequestFiltered(
			List<GroupRequest> myGroupRequestFiltered) {
		this.myGroupRequestFiltered = myGroupRequestFiltered;
	}

	/**
	 * @return the mailService
	 */
	public MailService getMailService() {
		return mailService;
	}

	/**
	 * @param mailService
	 *            the mailService to set
	 */
	public void setMailService(MailService mailService) {
		this.mailService = mailService;
	}

}
